package com.cx.bank.dao;

//import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.MysqlConnect;

import java.io.*;
import java.util.Properties;

/*
作者：龚俊豪
类名：BankDaoInterface
功能说明：
存储方法实现—saveMoney()
添加用户方法实现—insertUser()
更新方法说明实现—updateMoney()
按名字查找用户实现—findByName ()
按用户名和密码查找用户实现—findUser()
获取文件中money的值-getMoney()
向文件中的money添加-addMoney()
在getInstance上加锁
*/
public class BankDaoImpl implements BankDaoInterface{
    private static BankDaoImpl instance = null;
    private static MysqlConnect connect = new MysqlConnect();
    private BankDaoImpl(){}
//    加锁 锁的是他的反射对象
    public static synchronized BankDaoImpl getInstance(){
        if(instance == null){
            instance = new BankDaoImpl();
        }
        return instance;
    }

    @Override
    public void saveMoney(double money, UserBean user) {
//        MysqlConnect connect = new MysqlConnect();
        connect.addMoney(user,money);

    }

    @Override
    public void insertUser(String username,String password) {
//        MysqlConnect connect = new MysqlConnect();
        connect.InsertUser(username,password);
    }

    @Override
    public void deleteMoney(double money, UserBean user) {
//        MysqlConnect connect = new MysqlConnect();
        connect.reduceMoney(user,money);
    }

    @Override
    public UserBean findUserByName(String username) {
//        MysqlConnect connect = new MysqlConnect();
        UserBean user = connect.selectUserByName(username);
        return user;
    }

    @Override
    public double getMoney(String userName) {
//        MysqlConnect connect = new MysqlConnect();
        double money = connect.selectUserByName(userName).getMoney();
        return money;
    }

    @Override
    public void delete(String username) {
//        MysqlConnect connect = new MysqlConnect();
        connect.deleteUser(username);
    }

    @Override
    public int updateAdmin(UserBean user, int admin) {
//        MysqlConnect connect = new MysqlConnect();
        connect.updateAdmin(user,admin);
        return 0;
    }

}
